<?php
    
    require_once('Debug.lib.php');  
    require_once('Audit.lib.php');
    
    class MODEL_MAPI_Base {
    	  protected $format_json = TRUE;
        protected $log_name = 'mapi';
        private   $dbh_r=null;
        private   $dbh_w=null;
        protected $enable_audit = 1;
        protected $WWW   = null;
        protected $URL   = null;

        
        function __construct(){
        	 global $site_global_config;        	          
           $this->WWW = $site_global_config['WWW']; 
           $this->URL = $site_global_config['URL']; 
        }
        
        
        function log_error($message){
            if ($this->enable_audit != 1){
                return;
            }
            #$log_name = 'payment_error_'.$this->log_name;
            #echo "$log_name: $message\n";
            #debug_log('payment', "$log_name: $message");
            #?#$prefix = 'error_'.$this->log_name;
            #?#audit_log('CMS_Base', $prefix.': '.$message);
        }
        function log_operation($message){
            if ($this->enable_audit != 1){
                return;
            }
            #$log_name = 'payment_operation_'.$this->log_name;
            #echo "$log_name: $message\n";
            #debug_log('payment', "$log_name: $message");
            #?#$prefix = 'operation_'.$this->log_name;
            #?#audit_log('CMS_Base', $prefix.': '.$message);
        }
        
        # centralize the db connection class
        function db_connect($mode='read'){
            if ($mode == 'write'){
                if ($this->dbh_w == null){
                    $db = new DBI('write');
                    $this->dbh_w = $db->connect();
                }
                return $this->dbh_w;
            }
            
            if ($this->dbh_r == null){
                $db = new DBI('read');
                $this->dbh_r = $db->connect();   
            }
            return $this->dbh_r;
        } 
        
  protected function return_with_http_error($status_code, $content){
  	 header("{$_SERVER['SERVER_PROTOCOL']} $status_code");
  	 return $content;
  }       
        
  protected function return_api_error($error_code, $error_msg, $extra_data = array()){
  	return $this->return_with_http_error(400, json_encode(array('error'=>$error_code, 'error_msg'=>$error_msg)));
  }
        

  public function to_json($data=array(), $pageinfo=array(),$retcode=1,$msg='success'){  	
      if(!$data) $data = (object)$data;
      
  	  $result2 = array('retcode'=>$retcode,'msg'=>$msg,'data' => $data) ;
    
  	if (count($pageinfo) > 0){
  	  $result2 = array('retcode'=>$retcode,'msg'=>$msg,'data' => $data, 'pageinfo' => $pageinfo) ;
    }
  	header('Content-Type: text/plain'); # make it look better in browser output        	
  	
  	if ($this->format_json){
  		$util = getModel('Util');  		
  	  return $util->json_format(json_encode ( $result2 ));
    } else {
  	  return json_encode ( $result2 );
  	}
  }
  
  
        
    }
?>